package com.chance.cc.crawler.development.command.meta.oauth.weibo;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.development.command.meta.oauth.CrawlerOauthCommand;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2020/12/28 18:14
 * @Description
 *      微博cookie值放入数据库中
 **/
@Data
@AllArgsConstructor
public class WeiboUserAuthInfo {

    String category;
    String account;
    String cookie;

    public static void main(String[] args) throws IOException {
        List<String> cookieLines = IOUtils.readLines(new FileInputStream("F:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\oauth\\weibo\\weibo_oauth_info.txt"), Charset.forName("UTF-8"));

        List<Map<String,String>> allCookieInfoMapList = new ArrayList<>();
        for (String cookieLine : cookieLines) {
            String[] cookie = cookieLine.split(" ");
            Map<String,String> oauthInfoMap = new HashMap<>();
            oauthInfoMap.put("category",cookie[0]);
            oauthInfoMap.put("account",cookie[1]);
            oauthInfoMap.put("cookie",cookie[2]);

            Map<String,String> domainCookieInfoMap = new HashMap<>();
            domainCookieInfoMap.put("domain","weibo");
            domainCookieInfoMap.put("oauthInfo",JSON.toJSONString(oauthInfoMap));

            allCookieInfoMapList.add(domainCookieInfoMap);
        }

        CrawlerOauthCommand crawlerOauthCommand = new CrawlerOauthCommand("192.168.1.215",9599);


        HttpPage httpPage = crawlerOauthCommand.addOrUpdateUserOAuthInfo(allCookieInfoMapList);

        System.out.println(httpPage.getRawText());
    }
}
